//
//  MyCollectController.m
//  easystudy
//
//  Created by 七 on 16/9/4.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "MyCollectController.h"
#import "NewsListCell.h"
#import "ContentViewController.h"
@interface MyCollectController ()<UITableViewDelegate,UITableViewDataSource,NewsListCellDelegate>
{
    __weak IBOutlet UITableView *collectTable;
    __weak IBOutlet UIImageView *BGImageView;
    __weak IBOutlet UILabel *tipLbl1;
    __weak IBOutlet UILabel *tipLbl2;
    __weak IBOutlet UIView *navView;
    __weak IBOutlet UILabel *titleLbl;
    __weak IBOutlet UILabel *lineLbl;
    
    IBOutlet UIView *BGView;
    NSMutableArray *dataList;
    
}
@end

@implementation MyCollectController

- (void)viewDidLoad {
    [super viewDidLoad];
    navView.backgroundColor = KKColor_DarkGray_White;
    lineLbl.backgroundColor = KKColor_NavLineColor;
    BGView.backgroundColor = KKColor_BGColor_GrayToDarkgray;
    BGImageView.backgroundColor = KKColor_BGColor_GrayToDarkgray;
    tipLbl1.textColor = KKColor_Black_LightGary;
    tipLbl2.textColor = KKColor_Black_LightGary;
    dataList = [NSMutableArray array];
    if ([UserEntity sharedUser].UserID.length > 0) {
        NSDictionary *parmas = @{@"collect_cid":[[UserEntity sharedUser] getUserID]};
        [WebService findArticleCollect:parmas WithBlock:^(id result, NSError *error) {
            if (!error) {
                if ([result[@"errcode"] integerValue] == 0) {
                    NSArray *dataAry = result[@"result"][@"collect_list"][@"value"];
                    [dataList removeAllObjects];
                    [dataList addObjectsFromArray:dataAry];
                    if (dataList.count >0) {
                        [BGImageView setHidden:YES];
                        [tipLbl1 setHidden:YES];
                        [tipLbl2 setHidden:YES];
                        [collectTable reloadData];
                    }
                }else{
                    [self.view makeToast:[result[@"errmsg"] convertToString] position:@"center"];
                }
            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - User Action
- (IBAction)backPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark UITableViewDelegate & datasource
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *item = dataList[indexPath.row];
    NSString *imgString = [item[@"a_picture"] convertToString];
    NSArray *photoAry = [imgString componentsSeparatedByString:@","];
    NSInteger type = 0;
    if (photoAry.count >= 3) {
        type = 3;
    }else if (photoAry.count >= 1){
        type = 1;
    }
    switch (type) {
        case 0:
            return 85;
            break;
        case 1:
            return 90;
            break;
        case 3:
            return 170;
            break;
        default:
            break;
    }
    return 0;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"HomeClassCell";
    NewsListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[NewsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.dataIndex = indexPath.row;
    cell.delegate = self;
    NSDictionary *dict = dataList[indexPath.row];
    [cell setupNewsData:dict];
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *item = dataList[indexPath.row];
    ContentViewController *contentVC = [[ContentViewController alloc] init];
    NSDictionary *data = @{@"a_id":item[@"collect_aid"]};
    contentVC.dataInfo = data;
    contentVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:contentVC animated:YES];
}
@end
